package com.yt.website.filter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class WebsiteMemberLoginFilter implements HandlerInterceptor {
	private Log log = LogFactory.getLog(WebsiteMemberLoginFilter.class);

	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		log.debug("进入spring mvc拦截器");
		if(request.getSession().getAttribute("loginUser") == null){
			response.sendRedirect(getContextPath(request)+"/toLogin");
			return false;
		}
		return true;
	}

	/**
	 * 获取请求的相对的url
	 * 
	 * @param request
	 * @return
	 */
	private String getContextPath(HttpServletRequest request) {
		String uri = request.getContextPath();
		if(!"/".equals(uri)) {
			return uri;
		}else{
			return "";
		}
	}

	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {

	}

	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {

	}
}
